<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="文章详情"
    ></van-nav-bar>
    <!-- /导航栏 -->

    <!-- 加载中 loading -->
    <!-- <van-loading class="article-loading" /> -->
    <!-- /加载中 loading -->

    <!-- 文章详情 -->
    <div class="detail">
      <h3 class="title">{{ article.title }}</h3>
      <div class="author">
        <van-image
          round
          width="1rem"
          height="1rem"
          fit="fill"
          :src="article.aut_photo"
        />
        <div class="text">
          <p class="name">{{ article.aut_name }}</p>
          <p class="time">{{ article.pubdate | formatTime }}</p>
        </div>
        <van-button round size="small" type="info" @click="attention">{{
          article.is_followed ? '已关注' : '+关注'
        }}</van-button>
      </div>
      <div class="content">
        <!-- 直接解析html结构并渲染 -->
        <div v-html="article.content">正文</div>
      </div>
      <van-divider>END</van-divider>
      <div class="zan">
        <van-button
          round
          size="small"
          hairline
          type="primary"
          plain
          icon="good-job-o"
          >点赞</van-button
        >
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button
          round
          size="small"
          hairline
          type="danger"
          plain
          icon="delete"
          >不喜欢</van-button
        >
      </div>
    </div>
    <!-- /文章详情 -->
  </div>
</template>

<script>
// 导入文章详情
import {
  getArticleDateil,
  followUser,
  unfollowUser
} from '../../../api/article'
// 导入token工具函数
import { getToken } from '../../../utils/token'
export default {
  data () {
    // 文章正文信息
    return {
      article: ''
    }
  },
  created () {
    const id = this.$route.query.id
    console.log(id)
    getArticleDateil({ id }).then(res => {
      console.log(res)
      this.article = res.data.data
    })
  },
  methods: {
    // 关注取关功能
    attention () {
      // 判断是否登录
      if (getToken() === null) {
        this.$toast.success('请登录')
        return this.$router.push({
          path: '/login',
          query: {
            redirect: this.$route.fullPath
          }
        })
      }
      if (this.article.is_followed === false) {
        followUser({ target: this.article.aut_id }).then(res => {
          console.log(res)
          this.$toast.success('关注成功')
          this.article.is_followed = !this.article.is_followed
        })
      } else {
        unfollowUser({ target: this.article.aut_id }).then(res => {
          this.$toast.success('取关成功')
          this.article.is_followed = !this.article.is_followed
        })
      }
    }
  }
}
</script>

<style lang="less">
.article-container {
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
  .article-loading {
    padding-top: 100px;
    text-align: center;
  }
  .van-nav-bar {
    background-color: #3296fa;
    .van-nav-bar__title {
      color: white;
    }
    .van-icon-arrow-left {
      color: #fff;
    }
  }
  .error {
    padding-top: 100px;
    text-align: center;
  }
  .detail {
    padding: 50px 10px;
    .title {
      font-size: 16px;
    }
    .zan {
      text-align: center;
    }
    .author {
      padding: 10px 0;
      display: flex;
      .text {
        flex: 1;
        padding-left: 10px;
        line-height: 1.3;
        .name {
          font-size: 14px;
          margin: 0;
        }
        .time {
          margin: 0;
          font-size: 12px;
          color: #999;
        }
      }
    }
    .content {
      font-size: 14px;
      overflow: hidden;
      white-space: pre-wrap;
      word-break: break-all;
      ::v-deep img {
        max-width: 100%;
        background: #f9f9f9;
      }
    }
  }
}
</style>
